package com.allwees.bs.c.module.order.chain.create.handler;

import com.allwees.bs.c.module.order.chain.create.request.OrderCreateRequest;
import com.allwees.bs.c.module.order.chain.create.request.OrderItemCreateRequest;
import com.allwees.core.common.context.AbstractHandler;
import com.allwees.core.common.util.MoneyUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

@Component
public class CalculateOrderAmtHandler extends AbstractHandler<OrderCreateRequest> {

    @Value("${system.params.order.sales.taxRate:0}")
    private String taxRate;

    @Override
    public OrderCreateRequest handler(OrderCreateRequest request) {

        /** 商家订单金额 */
        BigDecimal merchantAmt = BigDecimal.ZERO;
        /** 平台订单金额 */
        BigDecimal platformAmt = BigDecimal.ZERO;

        BigDecimal amt = BigDecimal.ZERO, amtProduct = BigDecimal.ZERO, amtShipping = BigDecimal.ZERO, originalAmtSaleTax = BigDecimal.ZERO, amtSaleTax = BigDecimal.ZERO, amtSaleTaxAfterAdd = BigDecimal.ZERO,amtShippingAfterAdd = BigDecimal.ZERO,amtProductAfterAdd = BigDecimal.ZERO ;
        BigDecimal originalAmtShipping = BigDecimal.ZERO;
        BigDecimal originalAmtProduct = BigDecimal.ZERO;
        BigDecimal originalAmt =  BigDecimal.ZERO;
        for (OrderItemCreateRequest item : request.getItems()) {

            setAmtOf(item);

            amt = MoneyUtil.add(amt, item.getAmt());//实付总金额
            originalAmt = MoneyUtil.add(originalAmt, item.getOriginalAmt());//原始总金额
            platformAmt =  MoneyUtil.add(platformAmt,item.getPlatformAmtSku());
            merchantAmt = MoneyUtil.add(merchantAmt,item.getMerchantAmtSku());
            originalAmtShipping =  MoneyUtil.add(originalAmtShipping,item.getOriginalAmtShipping());
            originalAmtProduct =  MoneyUtil.add(originalAmtProduct,item.getOriginalAmtSku());
            amtShippingAfterAdd = MoneyUtil.add(amtShippingAfterAdd,item.getShippingAmtSkuAfterAdd());
            amtProductAfterAdd = MoneyUtil.add(amtProductAfterAdd,item.getProductAmtSkuAfterAdd());
            amtProduct = MoneyUtil.add(amtProduct, item.getAmtSku());//实付商品金额
            amtShipping = MoneyUtil.add(amtShipping, item.getAmtShipping());//实付运费金额
            originalAmtSaleTax = MoneyUtil.add(originalAmtSaleTax, item.getOriginalAmtSaleTaxSku());//原始税费
            amtSaleTaxAfterAdd = MoneyUtil.add(amtSaleTaxAfterAdd, item.getAmtSaleTaxSkuAfterAdd());//加价后税费
            amtSaleTax =  MoneyUtil.add(amtSaleTax,item.getAmtSaleTax());//实付税费
        }

        //===================以下三项是原始
        //原始税费
        request.setOriginalAmtSaleTax(originalAmtSaleTax);
        //原始运费金额
        request.setOriginalAmtShipping(originalAmtShipping);
        //原始商品金额
        request.setOriginalAmtProduct(originalAmtProduct);

        //===================以下三项是加价后
        //加价后税费
        request.setAmtSaleTaxAfterAdd(amtSaleTaxAfterAdd);
        //加价后运费金额
        request.setAmtShippingAfterAdd(amtShippingAfterAdd);
        //加价后商品金额
        request.setAmtProductAfterAdd(amtProductAfterAdd);

        //====================以下三项是实付
        //实付运费金额
        request.setAmtShipping(amtShipping);
        //实付商品金额
        request.setAmtProduct(amtProduct);
        //实付税费
        request.setAmtSaleTax(amtSaleTax);

        //=====================总金额
        //原始总金额
        request.setOriginalAmt(originalAmt);
        //实付总金额
        request.setAmt(amt);

        //商家应收金额
        request.setMerchantAmt(merchantAmt);
        //平台应收金额
        request.setPlatformAmt(platformAmt);

        return doNext(request);
    }

    private void setAmtOf(OrderItemCreateRequest item) {

        //=============以下四项是原始金额，不是实付
        item.setOriginalAmtSku(calculateAmtProductSku(item));//sku商品原始金额
        item.setOriginalAmtShippingSku(calculateAmtShippingSku(item));//sku运费原始金额
        item.setOriginalAmtShipping(calculateAmtShippingSku(item));//sku运费原始金额(保留字段)
        item.setOriginalAmtSaleTaxSku(calculateAmtSaleTaxSku(item));//sku商品的原始税费

        //========以下三项是计算加价后的金额
        item.setProductAmtSkuAfterAdd(calculateAmtProductSkuAfterAdd(item));//计算加价后的sku商品总金额
        item.setShippingAmtSkuAfterAdd(calculateAmtShippingSkuAfterAdd(item));//计算加价后的sku运费总金额
        item.setAmtSaleTaxSkuAfterAdd(calculateAmtSaleTaxSkuAfterAdd(item));// 计算加价后sku商品的总税费

        //============以下三项是实付
        item.setAmtSku(item.getProductAmtSkuAfterAdd());//sku实付商品金额,相当于amt_product_sku
        item.setAmtShipping(item.getShippingAmtSkuAfterAdd());//sku实付运费金额
        item.setAmtSaleTax(item.getAmtSaleTaxSkuAfterAdd());//sku实付税费

        //原始总金额 = sku商品原始金额 + sku运费原始金额 + sku商品的原始税费
        item.setOriginalAmt(MoneyUtil.add(item.getOriginalAmtSku(), item.getOriginalAmtShippingSku(), item.getOriginalAmtSaleTaxSku()));

        //实付总金额 =  sku商品实付金额 + sku运费实付金额 + sku商品实付税费
        item.setAmt(MoneyUtil.add(item.getAmtSku(),item.getAmtShipping(),item.getAmtSaleTax()));//实付总金额,在没有计算优惠之前就是原始总金额

        BigDecimal platformAmtSku = calculatePlatformAmtSku(item);
        item.setPlatformAmtSku(platformAmtSku);//sku平台订单金额
        item.setMerchantAmtSku(MoneyUtil.add(item.getOriginalAmtSku(),item.getOriginalAmtShippingSku()));//sku商家订单金额


    }

    /**
     * 计算sku商品的总金额
     * @param item
     * @return
     */
    private BigDecimal calculateAmtProductSku(OrderItemCreateRequest item) {
        return MoneyUtil.mul(item.getPrice(), BigDecimal.valueOf(item.getQuantity()));
    }


    /**
     * 计算sku商品的总税费
     * @param item
     * @return
     */
    private BigDecimal calculateAmtSaleTaxSku(OrderItemCreateRequest item){
        //获取费率配置值
        String value = this.taxRate;
        //费率
        BigDecimal salesTaxRate ;
        if (StringUtils.isBlank(value) || !NumberUtils.isCreatable(value)) {
            salesTaxRate = new BigDecimal(0);
        } else {
            salesTaxRate =  new BigDecimal(value);
        }
        //平台应收sku税费金额
        return MoneyUtil.mul(item.getPrice(),  salesTaxRate,BigDecimal.valueOf(item.getQuantity()));
    }

    /**
     * 计算加价后sku商品的总税费
     * @param item
     * @return
     */
    private BigDecimal calculateAmtSaleTaxSkuAfterAdd(OrderItemCreateRequest item){
        //获取费率配置值
        String value = this.taxRate;
        //费率
        BigDecimal salesTaxRate ;
        if (StringUtils.isBlank(value) || !NumberUtils.isNumber(value)) {
            salesTaxRate = new BigDecimal(0);
        } else {
            salesTaxRate =  new BigDecimal(value);
        }
        //平台应收sku税费金额
        return MoneyUtil.mul(item.getPriceAdjust(),  salesTaxRate,BigDecimal.valueOf(item.getQuantity()));
    }


    /**
     * 计算加价后的sku商品金额
     * @param item
     * @return
     */
    private BigDecimal calculateAmtProductSkuAfterAdd(OrderItemCreateRequest item) {
        return MoneyUtil.mul(item.getPriceAdjust(), BigDecimal.valueOf(item.getQuantity()));
    }
    /**
     * 计算加价后的sku运费金额
     * @param item
     * @return
     */
    private BigDecimal calculateAmtShippingSkuAfterAdd(OrderItemCreateRequest item) {
        return MoneyUtil.mul(item.getShippingPriceAdjust(), BigDecimal.valueOf(item.getQuantity()));
    }

    /**
     * 平台应收sku金额
     * @param item
     * @return
     */
    private BigDecimal calculatePlatformAmtSku(OrderItemCreateRequest item) {
        //平台应收sku运费金额
        BigDecimal amtShippingSku = item.getShippingAmtSkuAfterAdd();
        //平台应收sku商品金额
        BigDecimal amtProductSku = item.getProductAmtSkuAfterAdd();
        //平台应收sku税费金额
        BigDecimal amtSaleTaxSku = item.getAmtSaleTaxSkuAfterAdd();
        return MoneyUtil.add(amtShippingSku, amtProductSku,amtSaleTaxSku);
    }


    public BigDecimal calculateAmtShippingSku(OrderItemCreateRequest item) {
        return MoneyUtil.mul(item.getShippingPrice(), BigDecimal.valueOf(item.getQuantity()));
    }
}
